﻿using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Sql Server操作基类
/// </summary>
/// <author>yycmmc</author>
public sealed class SqlHelper
{
    private SqlHelper()
    { }

    #region 公共方法

    /// <summary>
    /// 验证字符串数组
    /// </summary>
    /// <param name="arr">字符串数组</param>
    /// <returns>验证结果:true or false</returns>
    private static bool ArrayIsNullOrEmpty(string[] arr)
    {
        if (arr == null || arr.Length < 1)
            return true;
        else
        {
            foreach (string parm in arr)
            {
                if (string.IsNullOrEmpty(parm))
                    return true;
            }
            return false;
        }
    }

    /// <summary>
    /// 检查参数
    /// </summary>
    /// <param name="parms">被检测的数组对象</param>
    /// <returns>检测结果:为空:true or false</returns>
    private static bool SqlParameterIsNullOrEmpty(SqlParameter[] parms)
    {
        if (parms == null || parms.Length <= 0)
        {
            return true;
        }
        else
        {
            foreach (SqlParameter parm in parms)
            {
                if (parm == null)
                    return true;
            }
            return false;
        }
    }

    /// <summary>
    /// 检查参数
    /// </summary>
    /// <param name="parms">被检测的数组对象</param>
    /// <returns>检测结果:为空:true or false</returns>
    private static bool SqlParameterIsNullOrEmpty(SqlParameter[][] parms)
    {
        if (parms == null || parms.Length <= 0)
        {
            return true;
        }
        else
        {
            for (int i = 0; i < parms.Length; i++)
            {
                if (parms[i] == null || parms[i].Length < 1)
                    return true;
                foreach (SqlParameter parm in parms[i])
                {
                    if (parm == null)
                        return true;
                }
            }
            return false;
        }
    }

    #endregion

    #region 执行存储过程

    #region 查询数据

    /// <summary>
    /// 带参数的存储过程查询
    /// </summary>
    /// <param name="procName">存储过程名</param>
    /// <param name="parms">参数</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>数据表</returns>
    public static DataTable Proc_ExecuteDataTable(string procName, SqlParameter[] parms, string conString)
    {
        bool isNull = SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(procName) || string.IsNullOrEmpty(conString);
        if (isNull)
            return null;
        DataTable dt = new DataTable();
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = conn;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = procName;
                foreach (SqlParameter parm in parms)
                    command.Parameters.Add(parm);
                try
                {
                    conn.Open();
                    using (SqlDataReader dr = command.ExecuteReader())
                    {
                        dt.Load(dr);
                    }
                    return dt;
                }
                catch
                {
                    return null;
                }
            }
        }
    }

    /// <summary>
    /// 不带参数的存储过程查询
    /// </summary>
    /// <param name="procName">存储过程名</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>数据表</returns>
    public static DataTable Proc_ExecuteDataTable(string procName, string conString)
    {
        if (string.IsNullOrEmpty(procName) || string.IsNullOrEmpty(conString))
            return null;
        DataTable dt = new DataTable();
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter(procName, conn))
            {
                adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                try
                {
                    adapter.Fill(dt);
                    return dt;
                }
                catch
                {
                    return null;
                }
            }
        }
    }

    /// <summary>
    /// 带参数的存储过程查询
    /// </summary>
    /// <param name="procName">存储过程名</param>
    /// <param name="parms">参数</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>数据集</returns>
    public static DataSet Proc_ExecuteDataSet(string procName, SqlParameter[] parms, string conString)
    {
        bool isNull = SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(procName) || string.IsNullOrEmpty(conString);
        if (isNull)
            return null;
        DataSet ds = new DataSet();
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand(procName, conn))
            {
                command.CommandType = CommandType.StoredProcedure;
                foreach (SqlParameter parm in parms)
                    command.Parameters.Add(parm);
                using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                {
                    try
                    {
                        adapter.Fill(ds);
                        return ds;
                    }
                    catch
                    {
                        return null;
                    }
                }
            }
        }
    }

    /// <summary>
    /// 带参数的存储过程查询
    /// </summary>
    /// <param name="procName">存储过程名</param>
    /// <param name="parms">参数</param>
    /// <param name="tbName">内存表表名</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>数据集</returns>
    public static DataSet Proc_ExecuteDataSet(string procName, SqlParameter[] parms, string tbName, string conString)
    {
        bool isNull = SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(procName) || string.IsNullOrEmpty(tbName) || string.IsNullOrEmpty(conString);
        if (isNull)
            return null;
        DataSet ds = new DataSet();
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand(procName, conn))
            {
                command.CommandType = CommandType.StoredProcedure;
                foreach (SqlParameter parm in parms)
                    command.Parameters.Add(parm);
                using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                {
                    try
                    {
                        adapter.Fill(ds, tbName);
                        return ds;
                    }
                    catch
                    {
                        return null;
                    }
                }
            }
        }
    }

    /// <summary>
    /// 不带参数的存储过程查询
    /// </summary>
    /// <param name="procName">存储过程名</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>数据集</returns>
    public static DataSet Proc_ExecuteDataSet(string procName, string conString)
    {
        if (string.IsNullOrEmpty(procName) || string.IsNullOrEmpty(conString))
            return null;
        DataSet ds = new DataSet();
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = conn;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = procName;
                using (SqlDataAdapter adapter = new SqlDataAdapter())
                {
                    adapter.SelectCommand = command;
                    try
                    {
                        adapter.Fill(ds);
                        return ds;
                    }
                    catch
                    {
                        return null;
                    }
                }
            }
        }
    }

    /// <summary>
    /// 不带参数的存储过程查询
    /// </summary>
    /// <param name="procName">存储过程名</param>
    /// <param name="tbName">内存表表名</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>数据集</returns>
    public static DataSet Proc_ExecuteDataSet(string procName, string tbName, string conString)
    {
        if (string.IsNullOrEmpty(procName) || string.IsNullOrEmpty(tbName) || string.IsNullOrEmpty(conString))
            return null;
        DataSet ds = new DataSet();
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = conn;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = procName;
                using (SqlDataAdapter adapter = new SqlDataAdapter())
                {
                    adapter.SelectCommand = command;
                    try
                    {
                        adapter.Fill(ds, tbName);
                        return ds;
                    }
                    catch
                    {
                        return null;
                    }
                }
            }
        }
    }

    #endregion

    #region 插入数据、更新数据、删除数据

    /// <summary>
    ///  执行插入、删除、更新操作
    /// </summary>
    /// <param name="proName">存储过程名</param>
    /// <param name="eCount">影响的行数</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool Proc_ExecuteNonQuery(string proName, out int eCount, string conString)
    {
        if (string.IsNullOrEmpty(proName) || string.IsNullOrEmpty(conString))
        {
            eCount = -2;
            return false;
        }
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand(proName, conn))
            {
                command.CommandType = CommandType.StoredProcedure;
                try
                {
                    conn.Open();
                    eCount = command.ExecuteNonQuery();
                    return true;
                }
                catch
                {
                    eCount = -1;
                    return false;
                }
            }
        }
    }

    /// <summary>
    ///  执行插入、删除、更新操作(附带事务)
    /// </summary>
    /// <param name="proName">存储过程名</param>
    /// <param name="eCount">影响的行数</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool Proc_ExecuteNonQueryWithTrans(string proName, out int eCount, string conString)
    {
        if (string.IsNullOrEmpty(proName) || string.IsNullOrEmpty(conString))
        {
            eCount = -2;
            return false;
        }
        using (SqlConnection conn = new SqlConnection(conString))
        {
            SqlTransaction transaction = null;
            using (SqlCommand command = new SqlCommand(proName, conn))
            {
                command.CommandType = CommandType.StoredProcedure;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    command.Transaction = transaction;
                    eCount = command.ExecuteNonQuery();
                    transaction.Commit();
                    return true;
                }
                catch
                {
                    if (transaction != null)
                        transaction.Rollback();
                    eCount = -1;
                    return false;
                }
            }
        }
    }

    /// <summary>
    /// 执行插入、删除、更新操作
    /// </summary>
    /// <param name="proName">存储过程名</param>
    /// <param name="parms">参数数组</param>
    /// <param name="eCount">影响的行数</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool Proc_ExecuteNonQuery(string proName, SqlParameter[] parms, out int eCount, string conString)
    {
        bool isNull = SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(proName) || string.IsNullOrEmpty(conString);
        if (isNull)
        {
            eCount = -2;
            return false;
        }
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand(proName, conn))
            {
                command.CommandType = CommandType.StoredProcedure;
                foreach (SqlParameter parm in parms)
                    command.Parameters.Add(parm);
                try
                {
                    conn.Open();
                    eCount = command.ExecuteNonQuery();
                    return true;
                }
                catch
                {
                    eCount = -1;
                    return false;
                }
            }
        }
    }

    /// <summary>
    /// 执行插入、删除、更新操作(附带事务)
    /// </summary>
    /// <param name="proName">存储过程名</param>
    /// <param name="parms">参数数组</param>
    /// <param name="eCount">影响的行数</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool Proc_ExecuteNonQueryWithTrans(string proName, SqlParameter[] parms, out int eCount, string conString)
    {
        bool isNull = SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(proName) || string.IsNullOrEmpty(conString);
        if (isNull)
        {
            eCount = -2;
            return false;
        }
        using (SqlConnection conn = new SqlConnection(conString))
        {
            SqlTransaction transaction = null;
            using (SqlCommand command = new SqlCommand(proName, conn))
            {
                command.CommandType = CommandType.StoredProcedure;
                foreach (SqlParameter parm in parms)
                    command.Parameters.Add(parm);
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    command.Transaction = transaction;
                    eCount = command.ExecuteNonQuery();
                    transaction.Commit();
                    return true;
                }
                catch
                {
                    eCount = -1;
                    transaction.Rollback();
                    return false;
                }
            }
        }
    }

    /// <summary>
    /// 执行多次不同插入、删除、更新操作(附带事务)
    /// </summary>
    /// <param name="proNames">存储过程名数组</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool Proc_ExecuteNonQueryWithTrans(string[] proNames, string conString)
    {
        bool isError = ArrayIsNullOrEmpty(proNames) || string.IsNullOrEmpty(conString);
        if (isError)
            return false;
        using (SqlConnection conn = new SqlConnection(conString))
        {
            SqlTransaction transaction = null;
            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = conn;
                command.CommandType = CommandType.StoredProcedure;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    command.Transaction = transaction;
                    foreach (string proName in proNames)
                    {
                        command.CommandText = proName;
                        command.ExecuteNonQuery();
                    }
                    transaction.Commit();
                }
                catch
                {
                    if (transaction != null)
                        transaction.Rollback();
                    return false;
                }
            }
            return true;
        }
    }

    /// <summary>
    /// 执行插入、删除、更新操作
    /// </summary>
    /// <param name="proName">存储过程名</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool Proc_ExecuteNonQuery(string proName, string conString)
    {
        if (string.IsNullOrEmpty(proName) || string.IsNullOrEmpty(conString))
            return false;
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand(proName, conn))
            {
                command.CommandType = CommandType.StoredProcedure;
                try
                {
                    conn.Open();
                    command.ExecuteNonQuery();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }

    /// <summary>
    /// 执行插入、删除、更新操作(附带事务)
    /// </summary>
    /// <param name="proName">存储过程名</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool Proc_ExecuteNonQueryWithTrans(string proName, string conString)
    {
        if (string.IsNullOrEmpty(proName) || string.IsNullOrEmpty(conString))
            return false;
        using (SqlConnection conn = new SqlConnection(conString))
        {
            SqlTransaction transaction = null;
            using (SqlCommand command = new SqlCommand(proName, conn))
            {
                command.CommandType = CommandType.StoredProcedure;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    command.Transaction = transaction;
                    command.ExecuteNonQuery();
                    transaction.Commit();
                    return true;
                }
                catch
                {
                    if (transaction != null)
                        transaction.Rollback();
                    return false;
                }
            }
        }
    }

    /// <summary>
    /// 执行插入、删除、更新操作
    /// </summary>
    /// <param name="proName">存储过程名</param>
    /// <param name="parms">参数数组</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool Proc_ExecuteNonQuery(string proName, SqlParameter[] parms, string conString)
    {
        bool isNull = SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(proName) || string.IsNullOrEmpty(conString);
        if (isNull)
            return false;
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand(proName, conn))
            {
                command.CommandType = CommandType.StoredProcedure;
                foreach (SqlParameter parm in parms)
                    command.Parameters.Add(parm);
                try
                {
                    conn.Open();
                    command.ExecuteNonQuery();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }

    /// <summary>
    /// 执行插入、删除、更新操作(附带事务)
    /// </summary>
    /// <param name="proName">存储过程名</param>
    /// <param name="parms">参数数组</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool Proc_ExecuteNonQueryWithTrans(string proName, SqlParameter[] parms, string conString)
    {
        bool isNull = SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(proName) || string.IsNullOrEmpty(conString);
        if (isNull)
            return false;
        using (SqlConnection conn = new SqlConnection(conString))
        {
            SqlTransaction transaction = null;
            using (SqlCommand command = new SqlCommand(proName, conn))
            {
                command.CommandType = CommandType.StoredProcedure;
                foreach (SqlParameter parm in parms)
                    command.Parameters.Add(parm);
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    command.Transaction = transaction;
                    command.ExecuteNonQuery();
                    transaction.Commit();
                    return true;
                }
                catch
                {
                    if (transaction != null)
                        transaction.Rollback();
                    return false;
                }
            }
        }
    }

    /// <summary>
    /// 执行插入、删除、更新操作(附带事务)
    /// </summary>
    /// <param name="proNames">存储过程名数组</param>
    /// <param name="parms">参数数组</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool Proc_ExecuteNonQueryWithTrans(string[] proNames, SqlParameter[][] parms, string conString)
    {
        bool isNull = ArrayIsNullOrEmpty(proNames) || SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(conString);
        if (isNull)
            return false;
        using (SqlConnection conn = new SqlConnection(conString))
        {
            SqlTransaction transaction = null;
            try
            {
                conn.Open();
                transaction = conn.BeginTransaction();
                for (int i = 0; i < proNames.Length; i++)
                {
                    SqlCommand command = new SqlCommand();
                    command.Connection = conn;
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = proNames[i];
                    command.Transaction = transaction;
                    foreach (SqlParameter parm in parms[i])
                        command.Parameters.Add(parm);
                    command.ExecuteNonQuery();
                }
                transaction.Commit();
            }
            catch
            {
                if (transaction != null)
                    transaction.Rollback();
                return false;
            }
            return true;
        }
    }

    #endregion

    #endregion

    #region 执行SQL语句

    #region 查询数据

    /// <summary>
    /// 不带参数的查询,用于获取一个数据表中的第一行(DataRow)
    /// </summary>
    /// <param name="selectSql">查询语句</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>数据表的第一行(DataRow)</returns>
    public static DataRow SQL_ExecuteDataRow(string selectSql, string conString)
    {
        DataTable dt = SQL_ExecuteDataTable(selectSql, conString);
        if (dt != null && dt.Rows.Count > 0)
            return dt.Rows[0];
        else
            return null;
    }

    /// <summary>
    /// 不带参数的查询，有于获取数据表中的第一列(DataColumn)
    /// </summary>
    /// <param name="selectSql">查询语句</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>数据表的第一列(DataColumn)</returns>
    public static DataColumn SQL_ExecuteDataColumn(string selectSql, string conString)
    {
        DataTable dt = SQL_ExecuteDataTable(selectSql, conString);
        if (dt != null && dt.Rows.Count > 0)
            return dt.Columns[0];
        else
            return null;
    }

    /// <summary>
    /// 不带参数的查询，用于获取数据表中的第一行中的第一列
    /// </summary>
    /// <param name="selectSql">查询语句</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>对象(Object)</returns>
    public static object SQL_ExecuteObjectData(string selectSql, string conString)
    {
        if (string.IsNullOrEmpty(selectSql) || string.IsNullOrEmpty(conString))
            return null;
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = conn;
                command.CommandType = CommandType.Text;
                command.CommandText = selectSql;
                try
                {
                    conn.Open();
                    return command.ExecuteScalar();
                }
                catch
                {
                    return null;
                }
            }
        }
    }

    /// <summary>
    /// 不带参数的查询，获取一个数据表(DataTable)
    /// </summary>
    /// <param name="selectSql">查询语句</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>数据表(DataTable)</returns>
    public static DataTable SQL_ExecuteDataTable(string selectSql, string conString)
    {
        if (string.IsNullOrEmpty(selectSql) || string.IsNullOrEmpty(conString))
            return null;
        DataTable dt = new DataTable();
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = conn;
                command.CommandType = CommandType.Text;
                command.CommandText = selectSql;
                try
                {
                    conn.Open();
                    using (SqlDataReader dr = command.ExecuteReader())
                    {
                        dt.Load(dr);
                    }
                    return dt;
                }
                catch
                {
                    return null;
                }
            }
        }
    }

    /// <summary>
    /// 带参的查询，用于获取一个数据表
    /// </summary>
    /// <param name="selectSql">查询语句</param>
    /// <param name="parms">参数</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>数据表(DataTable)</returns>
    public static DataTable SQL_ExecuteDataTable(string selectSql, SqlParameter[] parms, string conString)
    {
        bool isNull = SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(selectSql) || string.IsNullOrEmpty(conString);
        if (isNull)
            return null;
        DataTable dt = new DataTable();
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand(selectSql, conn))
            {
                foreach (SqlParameter parm in parms)
                    command.Parameters.Add(parm);
                using (SqlDataAdapter adpter = new SqlDataAdapter(command))
                {
                    try
                    {
                        adpter.Fill(dt);
                        return dt;
                    }
                    catch
                    {
                        return null;
                    }
                }
            }
        }
    }

    /// <summary>
    /// 不带参数的查询，返回一个数据集
    /// </summary>
    /// <param name="selectSql">查询语句</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>查询结果，数据集(DataSet)</returns>
    public static DataSet SQL_ExecuteDataSet(string selectSql, string conString)
    {
        if (string.IsNullOrEmpty(selectSql) || string.IsNullOrEmpty(conString))
            return null;
        DataSet ds = new DataSet();
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlDataAdapter adpter = new SqlDataAdapter(selectSql, conn))
            {
                try
                {
                    adpter.Fill(ds);
                    return ds;
                }
                catch
                {
                    return null;
                }
            }
        }
    }

    /// <summary>
    /// 不带参数的查询，返回一个数据集
    /// </summary>
    /// <param name="selectSql">查询语句</param>
    /// <param name="tbName">内存表表名</param>
    /// <param name="conString">数据库连接字符串</param>
    /// <returns>查询结果，数据集(DataSet)</returns>
    public static DataSet SQL_ExecuteDataSet(string selectSql, string tbName, string conString)
    {
        if (string.IsNullOrEmpty(selectSql) || string.IsNullOrEmpty(conString) || string.IsNullOrEmpty(tbName))
            return null;
        DataSet ds = new DataSet();
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlDataAdapter adpter = new SqlDataAdapter(selectSql, conn))
            {
                try
                {
                    adpter.Fill(ds, tbName);
                    return ds;
                }
                catch
                {
                    return null;
                }
            }
        }
    }

    /// <summary>
    /// 带参查询数据，返回一个数据集
    /// </summary>
    /// <param name="selectSql">查询语句</param>
    /// <param name="parms">参数数组</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>查询结果集，数据集(DataSet)</returns>
    public static DataSet SQL_ExecuteDataSet(string selectSql, SqlParameter[] parms, string conString)
    {
        bool isNull = SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(selectSql) || string.IsNullOrEmpty(conString);
        if (isNull)
            return null;
        DataSet ds = new DataSet();
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand(selectSql, conn))
            {
                foreach (SqlParameter parm in parms)
                    command.Parameters.Add(parm);
                using (SqlDataAdapter adpter = new SqlDataAdapter(command))
                {
                    try
                    {
                        adpter.Fill(ds);
                        return ds;
                    }
                    catch
                    {
                        return null;
                    }
                }
            }
        }
    }

    /// <summary>
    /// 带参查询数据，返回一个数据集
    /// </summary>
    /// <param name="selectSql">查询语句</param>
    /// <param name="parms">参数数组</param>
    /// <param name="tbName">内存表表名</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>查询结果集，数据集(DataSet)</returns>
    public static DataSet SQL_ExecuteDataSet(string selectSql, SqlParameter[] parms, string tbName, string conString)
    {
        bool isNull = SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(selectSql) || string.IsNullOrEmpty(tbName) || string.IsNullOrEmpty(conString);
        if (isNull)
            return null;
        DataSet ds = new DataSet();
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand(selectSql, conn))
            {
                foreach (SqlParameter parm in parms)
                    command.Parameters.Add(parm);
                using (SqlDataAdapter adpter = new SqlDataAdapter(command))
                {
                    try
                    {
                        adpter.Fill(ds, tbName);
                        return ds;
                    }
                    catch
                    {
                        return null;
                    }
                }
            }
        }
    }

    #endregion

    #region 插入数据、更新数据、删除数据

    /// <summary>
    ///  执行插入、删除、更新操作
    /// </summary>
    /// <param name="opSql">执行操作的SQL语句</param>
    /// <param name="eCount">影响的行数</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool SQL_ExecuteNonQuery(string opSql, out int eCount, string conString)
    {
        if (string.IsNullOrEmpty(opSql) || string.IsNullOrEmpty(conString))
        {
            eCount = -2;
            return false;
        }
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand(opSql, conn))
            {
                try
                {
                    conn.Open();
                    eCount = command.ExecuteNonQuery();
                    return true;
                }
                catch
                {
                    eCount = -1;
                    return false;
                }
            }
        }
    }

    /// <summary>
    ///  执行插入、删除、更新操作(附带事务)
    /// </summary>
    /// <param name="opSql">执行操作的SQL语句</param>
    /// <param name="eCount">影响的行数</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool SQL_ExecuteNonQueryWithTrans(string opSql, out int eCount, string conString)
    {
        if (string.IsNullOrEmpty(opSql) || string.IsNullOrEmpty(conString))
        {
            eCount = -2;
            return false;
        }
        using (SqlConnection conn = new SqlConnection(conString))
        {
            SqlTransaction transaction = null;
            using (SqlCommand command = new SqlCommand(opSql, conn))
            {
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    command.Transaction = transaction;
                    eCount = command.ExecuteNonQuery();
                    transaction.Commit();
                    return true;
                }
                catch
                {
                    if (transaction != null)
                        transaction.Rollback();
                    eCount = -1;
                    return false;
                }
            }
        }
    }

    /// <summary>
    /// 执行插入、删除、更新操作
    /// </summary>
    /// <param name="opSql">执行操作的sql语句</param>
    /// <param name="parms">参数数组</param>
    /// <param name="eCount">影响的行数</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool SQL_ExecuteNonQuery(string opSql, SqlParameter[] parms, out int eCount, string conString)
    {
        bool isNull = SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(opSql) || string.IsNullOrEmpty(conString);
        if (isNull)
        {
            eCount = -2;
            return false;
        }
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand(opSql, conn))
            {
                command.CommandType = CommandType.Text;
                foreach (SqlParameter parm in parms)
                    command.Parameters.Add(parm);
                try
                {
                    conn.Open();
                    eCount = command.ExecuteNonQuery();
                    return true;
                }
                catch
                {
                    eCount = -1;
                    return false;
                }
            }
        }
    }

    /// <summary>
    /// 执行插入、删除、更新操作(附带事务)
    /// </summary>
    /// <param name="opSql">执行操作的sql语句</param>
    /// <param name="parms">参数数组</param>
    /// <param name="eCount">影响的行数</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool SQL_ExecuteNonQueryWithTrans(string opSql, SqlParameter[] parms, out int eCount, string conString)
    {
        bool isNull = SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(opSql) || string.IsNullOrEmpty(conString);
        if (isNull)
        {
            eCount = -2;
            return false;
        }
        using (SqlConnection conn = new SqlConnection(conString))
        {
            SqlTransaction transaction = null;
            using (SqlCommand command = new SqlCommand(opSql, conn))
            {
                command.CommandType = CommandType.Text;
                foreach (SqlParameter parm in parms)
                    command.Parameters.Add(parm);
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    command.Transaction = transaction;
                    eCount = command.ExecuteNonQuery();
                    transaction.Commit();
                    return true;
                }
                catch
                {
                    eCount = -1;
                    transaction.Rollback();
                    return false;
                }
            }
        }
    }

    /// <summary>
    /// 执行多次不同插入、删除、更新操作(附带事务)
    /// </summary>
    /// <param name="opSqls">执行操作的sql语句数组</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool SQL_ExecuteNonQueryWithTrans(string[] opSqls, string conString)
    {
        bool isNull = ArrayIsNullOrEmpty(opSqls) || string.IsNullOrEmpty(conString);
        if (isNull)
            return false;
        using (SqlConnection conn = new SqlConnection(conString))
        {
            SqlTransaction transaction = null;
            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = conn;
                command.CommandType = CommandType.Text;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    command.Transaction = transaction;
                    foreach (string opSql in opSqls)
                    {
                        command.CommandText = opSql;
                        command.ExecuteNonQuery();
                    }
                    transaction.Commit();
                }
                catch
                {
                    if (transaction != null)
                        transaction.Rollback();
                    return false;
                }
            }
            return true;
        }
    }

    /// <summary>
    /// 执行多次不同插入、删除、更新操作
    /// </summary>
    /// <param name="opSqls">执行操作的sql语句数组</param>
    /// <param name="effectRowCout">影响的行数</param>
    /// <returns>执行结果:true or false</returns>
    public static bool SQL_ExecuteNonQueryWithTrans(string[] opSqls, out int effectRowCout, string conString)
    {
        bool isNull = ArrayIsNullOrEmpty(opSqls) || string.IsNullOrEmpty(conString);
        if (isNull)
        {
            effectRowCout = -1;
            return false;
        }
        using (SqlConnection conn = new SqlConnection(conString))
        {
            SqlTransaction transaction = null;
            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = conn;
                command.CommandType = CommandType.Text;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    command.Transaction = transaction;
                    effectRowCout = 0;
                    foreach (string opsql in opSqls)
                    {
                        command.CommandText = opsql;
                        effectRowCout += command.ExecuteNonQuery();
                    }
                    transaction.Commit();
                }
                catch
                {
                    effectRowCout = -1;
                    if (transaction != null)
                        transaction.Rollback();
                    return false;
                }
            }
            return true;
        }

    }

    /// <summary>
    /// 执行插入、删除、更新操作
    /// </summary>
    /// <param name="opSql">执行操作的sql语句</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool SQL_ExecuteNonQuery(string opSql, string conString)
    {
        if (string.IsNullOrEmpty(opSql) || string.IsNullOrEmpty(conString))
            return false;
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand(opSql, conn))
            {
                try
                {
                    conn.Open();
                    command.ExecuteNonQuery();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }

    /// <summary>
    /// 执行插入、删除、更新操作(附带事务)
    /// </summary>
    /// <param name="opSql">执行操作的sql语句</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool SQL_ExecuteNonQueryWithTrans(string opSql, string conString)
    {
        if (string.IsNullOrEmpty(opSql) || string.IsNullOrEmpty(conString))
            return false;
        using (SqlConnection conn = new SqlConnection(conString))
        {
            SqlTransaction transaction = null;
            using (SqlCommand command = new SqlCommand(opSql, conn))
            {
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    command.Transaction = transaction;
                    command.ExecuteNonQuery();
                    transaction.Commit();
                    return true;
                }
                catch
                {
                    if (transaction != null)
                        transaction.Rollback();
                    return false;
                }
            }
        }
    }

    /// <summary>
    /// 执行插入、删除、更新操作
    /// </summary>
    /// <param name="opSql">执行操作的SQL语句</param>
    /// <param name="parms">参数数组</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool SQL_ExecuteNonQuery(string opSql, SqlParameter[] parms, string conString)
    {
        bool isNull = SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(opSql) || string.IsNullOrEmpty(conString);
        if (isNull)
            return false;
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlCommand command = new SqlCommand(opSql, conn))
            {
                command.CommandType = CommandType.Text;
                foreach (SqlParameter parm in parms)
                    command.Parameters.Add(parm);
                try
                {
                    conn.Open();
                    command.ExecuteNonQuery();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }

    /// <summary>
    /// 执行插入、删除、更新操作(附带事务)
    /// </summary>
    /// <param name="opSql">执行操作的SQL语句</param>
    /// <param name="parms">参数数组</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool SQL_ExecuteNonQueryWithTrans(string opSql, SqlParameter[] parms, string conString)
    {
        bool isNull = SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(opSql) || string.IsNullOrEmpty(conString);
        if (isNull)
            return false;
        using (SqlConnection conn = new SqlConnection(conString))
        {
            SqlTransaction transaction = null;
            using (SqlCommand command = new SqlCommand(opSql, conn))
            {
                command.CommandType = CommandType.Text;
                foreach (SqlParameter parm in parms)
                    command.Parameters.Add(parm);
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    command.Transaction = transaction;
                    command.ExecuteNonQuery();
                    transaction.Commit();
                    return true;
                }
                catch
                {
                    if (transaction != null)
                        transaction.Rollback();
                    return false;
                }
            }
        }
    }

    /// <summary>
    /// 执行插入、删除、更新操作(附带事务)
    /// </summary>
    /// <param name="opSqls">sql 语句数组</param>
    /// <param name="parms">参数数组</param>
    /// <param name="conString">连接字符串</param>
    /// <returns>执行结果:true or false</returns>
    public static bool SQL_ExecuteNonQueryWithTrans(string[] opSqls, SqlParameter[][] parms, string conString)
    {
        bool isNull = ArrayIsNullOrEmpty(opSqls) || SqlParameterIsNullOrEmpty(parms) || string.IsNullOrEmpty(conString);
        if (isNull)
            return false;
        using (SqlConnection conn = new SqlConnection(conString))
        {
            SqlTransaction transaction = null;
            try
            {
                conn.Open();
                transaction = conn.BeginTransaction();
                for (int i = 0; i < opSqls.Length; i++)
                {
                    SqlCommand command = new SqlCommand();
                    command.Connection = conn;
                    command.CommandType = CommandType.Text;
                    command.CommandText = opSqls[i];
                    command.Transaction = transaction;
                    foreach (SqlParameter parm in parms[i])
                        command.Parameters.Add(parm);
                    command.ExecuteNonQuery();
                }
                transaction.Commit();
            }
            catch
            {
                if (transaction != null)
                    transaction.Rollback();
                return false;
            }
            return true;
        }
    }

    #endregion

    #endregion
}